/**
 * Created by limeng on 16/5/27.
 */
delegateApp.controller('ViewProposalCtrl', function (
    $scope, $rootScope, $http, $routeParams) {

    $scope.proposalId = $routeParams.proposalId;

    $scope.proposal = {};
    $scope.secondMotionList = [];
    $scope.secondMotionPageNo = 1;
    $scope.showMoreMotion = false;

    $http.get('/proposal/'
        + $scope.proposalId
    ).success(
        function (data) {
            if (data.state == true) {
                $scope.proposal = data.proposal;
                var contentElem = document.getElementById("proposalContent");
                contentElem.innerHTML = $scope.proposal.content;
                contentElem.style.padding = 15;
            } else {
                showAlert('查询失败', '原因是:'+data.result);
            }
        }
    ).error(
        function () {
            showAlert('查询失败', '请联系开发者进行维护');
        }
    );

    $scope.refreshMotionList = function() {
        $http.get('/second_motion/' + $scope.proposalId
            + '/10/' + ($scope.secondMotionPageNo++)
        ).success(
            function (data) {
                if (data.state == true) {
                    for (var i = 0; i < data.list.length; i++) {
                        $scope.secondMotionList.push(data.list[i]);
                    }
                    if ($scope.secondMotionPageNo <= data.pageCount) {
                        $scope.showMoreMotion = true;
                    } else {
                        $scope.showMoreMotion = false;
                    }
                } else {
                    showAlert('查询失败', '原因:'+data.result);
                }
            }
        ).error(
            function () {
                showAlert('查询失败', '附议信息数据库出现异常');
            }
        );
    };
    $scope.refreshMotionList();

    $scope.publishSecondMotion = function () {
        $http.post('/second_motion/',{
            authorId : $rootScope.delegate.id,
            proposalId : $scope.proposal.id,
            content : $scope.secondMotionContent
        }).success(function (data) {
            if (data.state == true) {
                bootbox.alert('发布成功, 您的附议已成功上传');
                $scope.secondMotionPageNo = 1;
                $scope.secondMotionList = [];
                $scope.refreshMotionList();
            } else {
                bootbox.alert('附议发布失败！, 错误为'+data.result);
            }
        }).error(function () {
            showAlert('附议发布失败！', '连接服务器失败，请联系维护者！');
        });
    };
    $scope.updateMotion = function (motion) {
        bootbox.prompt("修改:", function (res) {
            if (res != null) {
                $http.put("/second_motion/" + motion.id, {
                    content: res
                }).success(function (data) {
                    if (data.state == true) {
                        motion.content = res;
                    } else {
                        bootbox.alert("修改失败,错误为" + data.result);
                    }
                }).error(function () {
                    bootbox.alert("连接数据库失败,请联系维护者!");
                });
            }
        });
    };
    $scope.deleteMotion = function (motion) {
        bootbox.confirm("确定要删除本条附议吗?", function (res) {
            if (res == true) {
                $http.delete("/second_motion/" + motion.id).success(function (data) {
                    if (data.state == true) {
                        bootbox.alert("删除成功!");
                        $scope.secondMotionPageNo = 0;
                        $scope.secondMotionList = [];
                        $scope.refreshMotionList();
                    } else {
                        bootbox.alert("删除失败,错误为" + data.result);
                    }
                }).error(function () {
                    bootbox.alert("连接数据库失败,请联系维护者!");
                });
            }
        });
    }
});